package cn.hx.mall.permissions.controller;

import cn.hx.mall.permissions.entity.ResourceCategory;
import cn.hx.mall.permissions.service.ResourceCategoryService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


@Controller
@RequestMapping("/permissions")
public class ResourceCategoryController {

    @Resource
    private ResourceCategoryService resourceCategoryService;

    @GetMapping("/resourceCategory")
    public String resourceClassify(Model model){
        List<ResourceCategory> categoryList= resourceCategoryService.selectAll();
        model.addAttribute("categoryList",categoryList);
        return "permissions/resourceCategory";
    }


    @PostMapping("/addResourcesCategory")
    public String assResourcesCategory(ResourceCategory resourceCategory){
        resourceCategory.setCreateTime(new Date());
        System.out.println("添加"+resourceCategory);
            resourceCategoryService.insert(resourceCategory);
            return "redirect:/permissions/resourceCategory";
    }

    @PostMapping("/editResourcesCategory")
    public String editResourcesCategory(ResourceCategory resourceCategory){
        resourceCategoryService.update(resourceCategory);
        return "redirect:/permissions/resourceCategory";
    }

    @GetMapping("/deleteResourceCategory/{id}")
    public String deleteResourceCategory(@PathVariable Integer id){
        System.out.println(id);
        resourceCategoryService.deleteById((long)id);
        return "redirect:/permissions/resourceCategory";
    }


    @PostMapping("/findByIdCategory")
    @ResponseBody
    public ResourceCategory findByIdCategory(Integer id){
        System.out.println(id);
        ResourceCategory resourceCategory = resourceCategoryService.queryById((long) id);
        System.out.println(resourceCategory);
        return resourceCategory;
    }




}